package cn.ztuo.bitrade.controller.ore;

import cn.ztuo.bitrade.annotation.AccessLog;
import cn.ztuo.bitrade.constant.AdminModule;
import cn.ztuo.bitrade.constant.CommonStatus;
import cn.ztuo.bitrade.controller.BaseController;
import cn.ztuo.bitrade.entity.Cesspit;
import cn.ztuo.bitrade.entity.CesspitRecord;
import cn.ztuo.bitrade.entity.Coin;
import cn.ztuo.bitrade.service.CesspitRecordService;
import cn.ztuo.bitrade.service.CesspitService;
import cn.ztuo.bitrade.service.CoinService;
import cn.ztuo.bitrade.util.MessageResult;
import cn.ztuo.bitrade.vo.CesspitRecordVO;
import cn.ztuo.bitrade.vo.CesspitVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("ore")
@Slf4j
public class OreController extends BaseController {

    @Autowired
    private CesspitRecordService cesspitRecordService;
    @Autowired
    private CesspitService cesspitService;
    @Autowired
    private CoinService coinService;


    /**
     * 获取矿池列表
     * @param cesspitVO
     * @return
     */
    @RequiresPermissions("ore:page-query")
    @RequestMapping(value = "page-query",method = RequestMethod.POST)
    @AccessLog(module = AdminModule.ORE, operation = "分页查找ORE")
    public MessageResult getByPage(CesspitVO cesspitVO){
       Page<Cesspit> cesspits =  cesspitService.getPage(cesspitVO);
       return success(cesspits);
    }

    /**
     * 创建修改ORE
     * @param cesspit
     * @return
     */
    @RequiresPermissions("ore:save")
    @RequestMapping(value = "save",method = RequestMethod.POST)
    @AccessLog(module = AdminModule.ORE,operation = "新增修改ORE")
    public MessageResult saveOre(
            Cesspit cesspit){
        if(cesspit.getId()==null){
            //新增操作
            Coin coin = coinService.findOne(cesspit.getCoin().toUpperCase());
            if(coin == null){
                return error(400,"币种不存在");
            }else {
                if(coin.getStatus().equals(CommonStatus.ILLEGAL)) return error(400,"币种被禁用");
            }
            cesspit.setCreateTime(new Date());
            Cesspit ore = cesspitService.save(cesspit);
            if(ore.getId()>0){
                return success("创建矿机成功");
            }else{
                return error(400,"创建矿机失败");
            }
        }
        //修改操作
        cesspitService.save(cesspit);
        return success("更新成功");
    }

    /**
     * 获取用户列表
     * @param cesspitRecordVO
     * @return
     */
    @RequiresPermissions("ore:mylist")
    @RequestMapping(value = "mylist",method = RequestMethod.POST)
    @AccessLog(module = AdminModule.ORE,operation = "获取用户ORE列表")
    public MessageResult myList(CesspitRecordVO cesspitRecordVO){
        Page<CesspitRecord> ores = cesspitRecordService.getAllPage(cesspitRecordVO);
        return success(ores);
    }

}
